<script lang="ts" setup>
import { computed } from 'vue'
import { useStore } from '@/store'
import { useRoute } from 'vue-router'

const store = useStore()
const route = useRoute()

interface ITopSubCate {
  sub: { id: string; name: string }
  top: { id: string; name: string }
}

// 利用计算属性得到一级、二级类目的 名称 和 ID
const category = computed(() => {
  const cate = {} as ITopSubCate
  store.state.category.list.forEach((top) => {
    if (top.children) {
      const sub = top.children.find((sub) => sub.id === route.params.id)
      if (sub) {
        cate.top = { id: top.id, name: top.name }
        cate.sub = { id: sub.id, name: sub.name }
      }
    }
  })

  return cate
})
</script>

<template>
  <XtxBread>
    <XtxBreadItem to="/">首页</XtxBreadItem>
    <XtxBreadItem v-if="category.top" :to="`/category/${category.top.id}`">{{ category.top.name }}</XtxBreadItem>
    <Transition name="fade-right" mode="out-in">
      <XtxBreadItem v-if="category.sub" :key="category.sub.id">{{ category.sub.name }}</XtxBreadItem>
    </Transition>
  </XtxBread>
</template>

<style lang="scss" scoped></style>
